<?php 
App::uses('AppController', 'Controller');

class StallsController extends AppController {
	var $components = array('Link');
	public function index() {
		$this -> layout = 'layoutStalls';
	}

	/**
	* function view stalls
	* @author ThanhTran
	* @since 2014-11-25
	* @return 
	*/
	public function view($id = null,$short_url = null){
		$this->layout='layoutStalls';

		//get stall category
		$this->loadModel('ShopCategory');
		$shopCategory = $this->ShopCategory->findShopCategoryByShopId($id);
		$this->set('shopCategory',$shopCategory);

		//get shop by id
		$this->loadModel('Shop');
		$shop = $this->Shop->findShopById($id);
		$this->set('shop',$shop);
		if(empty($shop)){
			throw new NotFoundException();
		}

		//get 6 san pham moi nhat
		$this->loadModel('Product');
		$product = $this->Product->findLastestById($id);
		$this->set('productLastest',$product);
		$this->set('shop_id',$id);
		$this->set('short_url',$short_url);
		$this->set('title_for_layout',"Gian hang ".$shop['Shop']['shop_name']);
		//get 6 san pham ban chay
		$productbest = $this->Product->findBestProduct($id);
		$this->set('productbest',$productbest);

		//hổ trợ online
		$this->loadModel('ShopLocation');
		$shopLocation = $this->ShopLocation->findByShopId($id);
		$this->set('shopLocation',$shopLocation);

	}
	public function showAllBestProduct($id = null,$short_url = null)
	{
		$this->layout='layoutStalls';
		//get stall category
		$this->loadModel('ShopCategory');
		$shopCategory = $this->ShopCategory->findShopCategoryByShopId($id);
		$this->set('shopCategory',$shopCategory);
		//get shop by id
		$this->loadModel('Shop');
		$shop = $this->Shop->findShopById($id);
		$this->set('shop',$shop);
		if(empty($shop)){
			throw new NotFoundException();
		}
		//get tat ca san pham ban chay
		$this->loadModel('Product');
		$product = $this->Product->findAllBestProduct($id);
		$this->set('AllBestProduct',$product);
		$this->set('shop_id',$id);
		$this->set('short_url',$short_url);
		$this->set('title_for_layout',"Gian hang ".$shop['Shop']['shop_name']." - Sản phẩm mới nhất");

		//hổ trợ online
		$this->loadModel('ShopLocation');
		$shopLocation = $this->ShopLocation->findByShopId($id);
		$this->set('shopLocation',$shopLocation);
	}
	public function showAllLastest($id = null,$short_url = null)
	{
		$this->layout='layoutStalls';
		//get stall category
		$this->loadModel('ShopCategory');
		$shopCategory = $this->ShopCategory->findShopCategoryByShopId($id);
		$this->set('shopCategory',$shopCategory);
		//get shop by id
		$this->loadModel('Shop');
		$shop = $this->Shop->findShopById($id);
		$this->set('shop',$shop);
		if(empty($shop)){
			throw new NotFoundException();
		}
		//get tat ca san pham moi nhat
		$this->loadModel('Product');
		$product = $this->Product->findAllLastestById($id);
		$this->set('AllProductLastest',$product);
		$this->set('shop_id',$id);
		$this->set('short_url',$short_url);
		$this->set('title_for_layout',"Gian hang ".$shop['Shop']['shop_name']." - Sản phẩm mới nhất");
		//hổ trợ online
		$this->loadModel('ShopLocation');
		$shopLocation = $this->ShopLocation->findByShopId($id);
		$this->set('shopLocation',$shopLocation);
	}

	public function showProductSelling($id = null,$short_url = null,$short_url_category = null)
	{
		$this->layout='layoutStalls';
		//get stall category
		$this->loadModel('ShopCategory');
		$shopCategory = $this->ShopCategory->findShopCategoryByShopId($id);
		//get shop by id
		$this->loadModel('Shop');
		$shop = $this->Shop->findShopById($id);
		if(empty($shop)){
			throw new NotFoundException();
		}
		$this->loadModel('Category');
		$category = $this->Category->findByShortUrl($short_url_category);
		$category_id = $category['Category']['category_id'];
		//get san pham dang cung cap
		$this->loadModel('Product');
		$product = $this->Product->findSellingProduct($id,$category_id);
		$this->set('SellingProduct',$product);
		$this->set('shop_id',$id);

		$this->set('shop',$shop);
		$this->set('shopCategory',$shopCategory);
		$this->set('short_url',$short_url);
		$this->set('title_for_layout',"Gian hang ".$shop['Shop']['shop_name']." - Sản phẩm đang cung cấp");
		//hổ trợ online
		$this->loadModel('ShopLocation');
		$shopLocation = $this->ShopLocation->findByShopId($id);
		$this->set('shopLocation',$shopLocation);
	}
	public function showProductStock($id = null,$short_url = null,$short_url_category = null)
	{
		$this->layout='layoutStalls';
		//get shop by id
		$this->loadModel('Shop');
		$shop = $this->Shop->findShopById($id);
		if(empty($shop)){
			throw new NotFoundException();
		}
		$this->loadModel('Category');
		$category = $this->Category->findByShortUrl($short_url_category);
		$category_id = $category['Category']['category_id'];
		//get san pham trong kho-bai
		$this->loadModel('Product');
		$product = $this->Product->findStockProduct($id,$category_id);
		$this->set('StockProduct',$product);
		$this->set('shop_id',$id);

		$this->set('shop',$shop);
		//get stall category
		$this->loadModel('ShopCategory');
		$shopCategory = $this->ShopCategory->findShopCategoryByShopId($id);
		$this->set('shopCategory',$shopCategory);
		$this->set('short_url',$short_url);
		$this->set('title_for_layout',"Gian hang ".$shop['Shop']['shop_name']." - Sản phẩm trong kho bãi");
		//hổ trợ online
		$this->loadModel('ShopLocation');
		$shopLocation = $this->ShopLocation->findByShopId($id);
		$this->set('shopLocation',$shopLocation);
	}
	public function viewProduct($id, $short_url = null){
		$this->layout='layoutStalls';
		
		//get stall category
		$this->loadModel('ShopCategory');
		$this->loadModel('Product');
		$product = $this->Product->findById($id);
		$shopCategory = $this->ShopCategory->findShopCategoryByShopId($product['Product']['shop_id']);
		//get shop by id
		$this->loadModel('Shop');
		$shop = $this->Shop->findShopById($product['Product']['shop_id']);
		if(empty($shop)){
			throw new NotFoundException();
		}
		$this->set('shop',$shop);

		$this->set('shopCategory',$shopCategory);
		$this->set('short_url',$short_url);
		$this->set('product',$product);
		$this->set('title_for_layout',"Sản phẩm ".$product['Product']['product_name']);
		//hổ trợ online
		$this->loadModel('ShopLocation');
		$shopLocation = $this->ShopLocation->findByShopId($id);
		$this->set('shopLocation',$shopLocation);
	}	
}	
?>